package hazesoft.Collection;

import hazesoft.Collection.R.id;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import java.io.*;

import android.widget.TextView;


public class MessageAdapter extends ArrayAdapter<messages_type> {

	private ArrayList<messages_type> items;
	private Context context;
	
	public MessageAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		this.items = items;
		GetMessages();
		// TODO Auto-generated constructor stub
	}
	
	@Override 
	public View getView(int Position, View convertView, ViewGroup Parent)
	{
		View v = convertView;
		if(v== null)
		{			
			LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.message_list_item, null);
			messages_type temp = items.get(Position);
			
			if(temp != null)
			{
				TextView From = (TextView)v.findViewById(id.lblFrom);
				TextView Body = (TextView)v.findViewById(id.lblBody);
				
				if(From != null)
					From.setText(temp.GetFrom());
				
				if(Body != null)
					Body.setText(temp.GetBody());
					
			}
		}
		
		return v;
	}
	
	private void GetMessages()
	{
		String[] Files = getContext().fileList(); 
		messages_type temp = new messages_type();
		for(int x = 0; x < Files.length ; x++)
		{
			if(Files[x].contains(".msg") == true)
			{
				FileInputStream fIn;
				try
				{
					fIn = getContext().openFileInput(Files[x]);
				
				InputStreamReader in = new InputStreamReader(fIn);
				BufferedReader br = new BufferedReader(in);
				
				temp.SetFrom(br.readLine());
				temp.SetBody(br.readLine());
		
				br.close();
				in.close();
				fIn.close();
				}
				catch(Exception e)
				{
					
				}
				items.add(temp);
			}
		}
	}
	

}
